home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Frameworks / Extension Shell 1.3 / Sample Extensions / MacCough ƒ / MacCough.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-06  |  7.7 KB  |  292 lines  |  [TEXT/R*ch]

  1. /*    NAME:
  2.         MacCough.c
  3.  
  4.     WRITTEN BY:
  5.         Dair Grant
  6.                 
  7.     DESCRIPTION:
  8.         This file contains a CODE resource to be used as a handler by
  9.         Extension Shell.
  10.  
  11.     ___________________________________________________________________________
  12.  
  13.     VERSION HISTORY:
  14.         (Mar 1994, dg)
  15.             •    First publicly distributed version.
  16.  
  17.  
  18.     ___________________________________________________________________________
  19. */
  20. //=============================================================================
  21. //        Include files                                                                     
  22. //-----------------------------------------------------------------------------
  23. #include <GestaltEqu.h>
  24. #include <Traps.h>
  25. #include "ParamBlock.h"
  26. #include "StandaloneCode.h"
  27. #include "ESConstants.h"
  28. #include "CodeConstants.h"
  29. #include "MacCough.h"
  30. #include "MCAddrsTable.h"
  31.  
  32.  
  33.  
  34.  
  35.  
  36. //=============================================================================
  37. //        Private function prototypes                                                                     
  38. //-----------------------------------------------------------------------------
  39. void    main(short theMsg, ESParamBlock *theParamBlock);
  40. void    InitialiseParamBlock(void);
  41. void    InitialiseAddrsTable(void);
  42. void    HandleTheError(void);
  43. void    SetUpIcons(int animDelay, int numIcons, int firstIcon);
  44.  
  45.  
  46.  
  47.  
  48.  
  49. //=============================================================================
  50. //        Global variables                                                                 
  51. //-----------------------------------------------------------------------------
  52. ESParamBlock    *gTheParamBlock;
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63. //=============================================================================
  64. //        main : Entry point to our code resource.                                                                 
  65. //-----------------------------------------------------------------------------
  66. //        Note :    Extension Shell communicates with us via a message constant,
  67. //                and a pointer to a structure it owns. Our job is to fill in
  68. //                the details, depending on what it wants us to do. It takes
  69. //                care of the rest.
  70. //-----------------------------------------------------------------------------
  71. void main(short theMsg, ESParamBlock *theParamBlock)
  72. {
  73.  
  74.  
  75.  
  76.  
  77.     // Set up A4 so that we can access our globals, and initialise them.
  78.     GetGlobals();
  79.     gTheParamBlock = theParamBlock;
  80.  
  81.  
  82.  
  83.     // Case out on what we have to do
  84.     switch(theMsg) {
  85.         case kInitialiseParamBlock:
  86.              InitialiseParamBlock();
  87.              break;
  88.              
  89.         case kInitialiseAddrsTable:
  90.              InitialiseAddrsTable();
  91.              break;
  92.  
  93.         case kHandleError:
  94.              HandleTheError();
  95.              break;
  96.     
  97.         default:
  98.              ;
  99.     }
  100.  
  101.  
  102.  
  103.     // Restore A4.
  104.     UngetGlobals();
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116. //=============================================================================
  117. //        InitialiseParamBlock : Initialises the ParamBlock.                                                                 
  118. //-----------------------------------------------------------------------------
  119. //        Note :    We have three things we want to do:
  120. //                    • Check to see if we can still run
  121. //                    • Set up the icons we want to display
  122. //                    • Set up the code we want installed
  123. //-----------------------------------------------------------------------------
  124. void InitialiseParamBlock(void)
  125. {    int        i;
  126.  
  127.  
  128.  
  129.  
  130.     // Check for System 7. We depend on having System 7, and won't
  131.     // run if we don't have it. We beep, post an error message,
  132.     // and show our disabled icon(s).
  133.     if (gTheParamBlock->systemVersion < 0x0700)
  134.         {
  135.         // Error details
  136.         gTheParamBlock->beepNow                = true;
  137.         gTheParamBlock->postError            = true;
  138.         gTheParamBlock->errorStringsID        = kErrorStrings;
  139.         gTheParamBlock->errorStringIndex    = kNeedSystemSeven;
  140.  
  141.  
  142.         // Icon details
  143.         SetUpIcons(kDisabledAnimDelay, kMyNumDisabledIcons, kMyFirstDisabledIcon);
  144.         }
  145.     
  146.     
  147.     
  148.     // If a shift key, or the mouse button, is down, we don't load either.
  149.     // We don't post an error, but we do show our disabled icon(s).
  150.     else if ((*gTheParamBlock->UserForcedDisable)(kShiftKey, true))
  151.         {
  152.         // Icon details
  153.         SetUpIcons(kDisabledAnimDelay, kMyNumDisabledIcons, kMyFirstDisabledIcon);
  154.         }
  155.     
  156.     
  157.     
  158.     // Otherwise, we're allowed to run so we show our icon(s) as normal,
  159.     // and fill in the details for the code we want installed.
  160.     else
  161.         {
  162.         // Icon details
  163.         SetUpIcons(kEnabledAnimDelay, kMyNumEnabledIcons, kMyFirstEnabledIcon);
  164.         
  165.         
  166.         // We install one time manager task, and request an address table
  167.         gTheParamBlock->installAddressTable        = true;
  168.         gTheParamBlock->addressTableSelector    = kMacCoughAddressTable;
  169.         gTheParamBlock->numCodeResources        = 1;
  170.  
  171.  
  172.         // Details for a Time Manager Task. We prime it to go off in 60 seconds,
  173.         // remembering that the Time Manager is fully functioning when we're
  174.         // called. 60 seconds should be long enough for the Finder to start up.
  175.         gTheParamBlock->theCodeResources[kTimeTask].resType        = kTimeTaskResType;
  176.         gTheParamBlock->theCodeResources[kTimeTask].resID        = kTimeTaskResID;
  177.         gTheParamBlock->theCodeResources[kTimeTask].codeType    = kTimeManagerTaskType;
  178.         gTheParamBlock->theCodeResources[kTimeTask].theCodeThing.theTimeManagerTask.theDelay = 60000;
  179.         }
  180. }
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191. //=============================================================================
  192. //        InitialiseAddrsTable : Initialise the address table.                                                     
  193. //-----------------------------------------------------------------------------
  194. //        Note :    If we are being used in a Control Panel, we will probably have
  195. //                implemented the address table code with a custom code resource
  196. //                that returns a structure with an address table embedded at the
  197. //                start. This function's job is to correctly initialise the
  198. //                extended fields of that structure. If we're not using a
  199. //                (custom) address table then we don't do anything.
  200. //
  201. //                The message for this routine will only arrive if we've
  202. //                requested an address table.
  203. //-----------------------------------------------------------------------------
  204. void InitialiseAddrsTable(void)
  205. {    MCAddressTable    *theAddressTable;
  206.     Handle            theHnd;
  207.  
  208.     
  209.  
  210.  
  211.     // Our custom address table holds one ProcPtrs, and a Handle. The ProcPtr
  212.     // is used by Extension Shell for Time Manager task, and the last field
  213.     // is a handle to the noise we will play. First we call Gestalt to get the
  214.     // address of the table.
  215.     Gestalt(kMacCoughAddressTable, &theAddressTable);
  216.  
  217.     
  218.     
  219.     // Then we load the sound, detach it, and store the handle in the table.
  220.     theHnd = GetResource('snd ', kTheCoughNoise);
  221.     DetachResource(theHnd);
  222.     theAddressTable->theSound = theHnd;
  223. }
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234. //=============================================================================
  235. //        HandleTheError : Handle any errors                                                             
  236. //-----------------------------------------------------------------------------
  237. //        Note :    If any error occurs, we beep, post an error, and remove our
  238. //                code. We also have to reset the icon details to show our
  239. //                disabled icons.
  240. //-----------------------------------------------------------------------------
  241. void HandleTheError(void)
  242. {
  243.  
  244.  
  245.  
  246.  
  247.     // General error handling settings
  248.     gTheParamBlock->removeInstalledCode    = true;
  249.     gTheParamBlock->beepNow                = true;
  250.     gTheParamBlock->postError            = true;
  251.     gTheParamBlock->errorStringsID        = kErrorStrings;
  252.  
  253.  
  254.  
  255.     // Icon details
  256.     SetUpIcons(kDisabledAnimDelay, kMyNumDisabledIcons, kMyFirstDisabledIcon);
  257.  
  258.  
  259.  
  260.     // Just give a general error.
  261.     gTheParamBlock->errorStringIndex = kUnknownError;
  262. }
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273. //=============================================================================
  274. //        SetUpIcons : Set up our icons accordingly.                                                         
  275. //-----------------------------------------------------------------------------
  276. //        Note :    We are passed in the resource ID of the first icon, the number
  277. //                of icons, and a delay for animation. We just fill in the fields
  278. //                in the paramBlock.
  279. //-----------------------------------------------------------------------------
  280. void SetUpIcons(int animDelay, int numIcons, int firstIcon)
  281. {    int        i;
  282.  
  283.  
  284.  
  285.  
  286.     // Fill in the fields
  287.     gTheParamBlock->animationDelay    = animDelay;
  288.     gTheParamBlock->numIcons        = numIcons;
  289.     for (i = 1; i <= numIcons; i++)
  290.         gTheParamBlock->theIcons[i] = firstIcon + i - 1;
  291. }
  292.